﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;

namespace Snake.Client
{
    public class Player
    {
        public Player(Body snakeBody, Controls controls, string playerName)
        {
            Body = snakeBody;
            Controls = controls;
            Health = new Health();
            Name = playerName;
        }

        public Controls Controls { get; private set; }
        public Body Body { get; private set; }
        public Health Health { get; private set; }
        public bool Dead { get; set; }
        public int Points { get; private set; }
        public string Name { get; private set; }
        
        public void ProcessFoodCollecting(Food food)
        {
            if (Body.FoodCollision(food))
            {
                Points++;
                food.ChangePosition();
                Body.Enlarge();
                Sound.PlayFoodCollectedSound();
            }
        }
        public void ProcessCollisions(List<Player> _playersList)
        {
            bool collided = false;

            if (Body.SelfCollision())
            {
                collided = true;
                Health.Hurt();
            }
            if (_CollidesWithOtherPlayers(_playersList))
            {
                collided = true;
                Health.Hurt();
            }
            if (!collided)
            {
                Health.Heal();
            }
            if (Health.Zero)
            {
                Dead = true;
            }
        }

        private bool _CollidesWithOtherPlayers(List<Player> playersList)
        {
            foreach (var player in playersList)
            {
                if (!ReferenceEquals(this, player))
                {
                    if (Body.CollidesWithBody(player.Body))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
